package com.example.esc_im_client.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import com.example.esc_im_client.db.sql_enuim.sqlFactory.sqlEnum.SQLTableEnum;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: SQLiteOpenHelper
 * @Description:
 * @Author: 墨渍
 * @Date: 2023/9/23 21:30
 **/
public class DatabaseHelper<T> extends SQLiteOpenHelper {
    private static final String DATABASE_NAME = "esc-client";
    private static final int DATABASE_VERSION = 1;


    public DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        SQLTableEnum[] values = SQLTableEnum.values();
        for (SQLTableEnum sqlEnum : values){
            String sql = sqlEnum.getSql();
            // 在这里创建数据库表
            try {
                Log.d("创建数据库","开始创建数据库，执行语句为"+ sql);
                db.execSQL(sql);
            }catch (Exception ex){
                Log.e("error",ex.getMessage());
                ex.printStackTrace();
            }

        }

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // 升级数据库表的逻辑，如果需要的话
    }

    // 执行自定义的 SQL 语句
    public void executeSQL(String sql) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.execSQL(sql);
        Log.d("执行sql", "执行sql，sql为" + sql);
        db.close();
    }

    @Override
    public SQLiteDatabase getReadableDatabase() {
        return super.getReadableDatabase();
    }

    public void getCursor(Cursor cursor){
        List<T> list = new ArrayList<>();
        String[] columnNames = cursor.getColumnNames();

    }




    public T getObj(Class<T> T, Cursor cursor, String[] columnNames) throws IllegalAccessException, InstantiationException {
        T result = T.newInstance();
        while (cursor.moveToNext()){
            for (String columnName : columnNames) {
                setMethod(result, columnName, cursor);
            }
        }
        return result;
    }



    /**
     * 调用set方法将数据插入输出模型
     * @param result 模型类对象
     * @param paramName 修改字段名称
     * @param cursor 入参的对象内容
     */
    public void setMethod(T result, String paramName, Cursor cursor){
        Class<?> objClass = result.getClass();
        String setParamName = paramName.substring(0, 1).toUpperCase() + paramName.substring(1);
        try {
            Field declaredField = objClass.getDeclaredField(paramName);
            declaredField.setAccessible(true);
            Class<?> paramClass = declaredField.getType();

            if(String.class == paramClass){
                Method method = objClass.getMethod("set" + setParamName, String.class);
                method.invoke(result, cursor.getString(cursor.getColumnIndexOrThrow(paramName)));
            } else if (Integer.class == paramClass) {
                Method method1 = objClass.getMethod("set" + setParamName, Integer.class);
                method1.invoke(result,cursor.getInt(cursor.getColumnIndexOrThrow(paramName)));
            } else if (Double.class == paramClass) {
                Method method2 = objClass.getMethod("set" + setParamName, Double.class);
                method2.invoke(result,cursor.getDouble(cursor.getColumnIndexOrThrow(paramName)));
            } else if (Long.class == paramClass) {
                Method method3 = objClass.getMethod("set" + setParamName, Long.class);
                method3.invoke(result,cursor.getLong(cursor.getColumnIndexOrThrow(paramName)));
            } else if (Short.class == paramClass) {
                Method method4 = objClass.getMethod("set" + setParamName, Short.class);
                method4.invoke(result,cursor.getShort(cursor.getColumnIndexOrThrow(paramName)));
            } else if (Boolean.class == paramClass) {
                Method method5 = objClass.getMethod("set" + setParamName, Boolean.class);
                method5.invoke(result,cursor.getExtras().getBoolean(String.valueOf(cursor.getColumnIndexOrThrow(paramName))));
            }else {
                throw new RuntimeException("类型错误");
            }

        }catch (NoSuchFieldException noSuchFieldException){
            Log.e("参数获取","错误信息为" + noSuchFieldException.getMessage());
            Log.e("参数获取", "参数获取失败, 参数名称为 ："+ paramName + "获取的类为" + result);
        }catch (NoSuchMethodException noSuchMethodException){
            Log.e("set方法获取","错误信息为" + noSuchMethodException.getMessage());
            Log.e("set方法获取", "方法获取失败, 参数名称为 ："+ paramName + "获取的类为" + result);
        } catch (InvocationTargetException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

}
